package com.ccg.controller;

import com.ccg.entity.Message;
import com.ccg.entity.User;
import com.ccg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;


@Controller
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/findUserById")
    @ResponseBody
    public Message findUserById(@RequestParam(defaultValue = "-1") int u_id) {
        User user = userService.findUserById(u_id);
        if (user != null) return new Message(0, "查找用户成功", user);
        else return new Message(1, "找不到该" + u_id + "的用户", null);
    }

    //根据用户名精确查找用户
    @RequestMapping("/findUserByUsername")
    @ResponseBody
    public Message findUserByUsername(@RequestParam(defaultValue = "") String username) {
        if (username != "" && username != null) {
            User user = userService.findUserByUsername(username.trim());
            if (user != null) return new Message(0, "查找用户成功", user);
            else return new Message(1, "找不到该" + username + "的用户", null);
        }
        return new Message(1, "找不到该" + username + "的用户", null);
    }

    //根据email精确查找用户
    @RequestMapping("/findUserByEmail")
    @ResponseBody
    public Message findUserByEmail(@RequestParam(defaultValue = "") String email) {
        if (email != ""&&email!=null) {
            User user = userService.findUserByEmail(email.trim());
            if (user != null) return new Message(0, "查找用户成功", user);
            else return new Message(1, "找不到该邮箱地址" + email + "的用户", null);
        }
        return new Message(1, "找不到该邮箱地址" + email + "的用户", null);
    }
    //冻结用户

    //查找所有被冻结的用户
    @RequestMapping("/findAllLocked")
    @ResponseBody
    public Message findAllLocked(){
        return userService.findAllLocked();
    }
    //对用户进行冻结或者解冻
    @RequestMapping("/updateLocked")
    @ResponseBody
    public Message updateLocked(@RequestBody User user){
        return userService.updateLocked(user);
    }
    //分页查找所有用户
    @RequestMapping("/findAllUser")
    @ResponseBody
    public Message findAllUser(int currentPage){
        return new Message(1,"查找成功",userService.findAllUser(currentPage));
    }

    //统计
    //查找所有用户
    @RequestMapping("/getUserCountInDate")
    @ResponseBody
    public Message getUserCountInDate(String startDate,String endDate){
        HashMap<String, Object> map = new HashMap<>();//哈希集合存储不重复的数据
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        return new Message(1,"查找成功",userService.getUserCountInDate(map));
    }

}
